package main

import (
	"fmt"
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"strconv"
	"time"
)

type Integer int

func (a Integer) compare( b int)

func main() {

	var testStr = "test string "

	fmt.Println("the length of the testStr ", len(testStr))

	// for i := 0; i < len(testStr); i++ {
	// 	fmt.Println(i," is ", string(testStr[i]))
	// }

	// for i, v := range testStr {
	// 	fmt.Println(i," is ", string(v))
	// }

	testArray := [5]int{1,2,3,4,5}

	modifyArrayTest(testArray)

	fmt.Println("in main", testArray)

	testArray1 := [10]int{1,2,3,4,5,6,7,8,9,10}

	var testSlice1 []int = testArray1[:6]

	//var testslice2 []int = make([]int,5)

	modifySliceTest(testSlice1)

	fmt.Println("in main", testSlice1)

	fmt.Println(" ***************** chan test **********************")

	var ch chan int
	ch = make(chan int)

	go add(1,2,ch)

	z := <-ch
	fmt.Println("in main", z)



	t := make(chan bool)

	go timeout(t)

	var tt bool
	ch1 := make(chan int, 1)

	for {
		select {
			case ch1 <- 0:
			case ch1 <- 1:
			case tt = <-t :
				fmt.Println("break")
		}
		
		if tt {
			break
		}

		i := <-ch1
		fmt.Println("Value received:", i)
		
	}
}

func timeout(t chan bool) {
	time.Sleep(10)
	t <- true
}

func add(a, b int, ch chan int) {
	z := a + b
	ch <- z
	fmt.Println("in function ",z)
}

func modifySliceTest(s []int) {
	s[0] = 10
	fmt.Println("in fucntion", s)
}

func modifyArrayTest(a [5]int) {
	a[0] = 10
	fmt.Println("in function", a)
}


func testMysql() {

	db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/ssp_online_test?charset=utf8")

	if err != nil {
		fmt.Println(err)
		return
	}

	defer db.Close()

	stmt, err1 := db.Prepare("select advertiser_id from blockrule_advertiser where blockrule_id = ?")

	if err1 != nil {
		fmt.Println(err1)
		return
	}

	defer stmt.Close()

	var value int 
	if stmt != nil {
		rows, _ := stmt.Query(216)
		for rows.Next() {
			rows.Scan(&value)
			fmt.Println("mysql result = " + strconv.Itoa(value))
		}
		
	}
}

/*
func printResult(query *sql.Rows) {  
        column, _ := query.Columns()              //读出查询出的列字段名  
        values := make([][]byte, len(column))     //values是每个列的值，这里获取到byte里  
        scans := make([]interface{}, len(column)) //因为每次查询出来的列是不定长的，用len(column)定住当次查询的长度  
        for i := range values {                   //让每一行数据都填充到[][]byte里面  
                scans[i] = &values[i]  
        }  
        results := make(map[int]map[string]string) //最后得到的map  
        i := 0  
        for query.Next() { //循环，让游标往下移动  
                if err := query.Scan(scans...); err != nil { //query.Scan查询出来的不定长值放到scans[i] = &values[i],也就是每行都放在values里  
                        fmt.Println(err)  
                        return  
                }  
                row := make(map[string]string) //每行数据  
                for k, v := range values {     //每行数据是放在values里面，现在把它挪到row里  
                        key := column[k]  
                        row[key] = string(v)  
                }  
                results[i] = row //装入结果集中  
                i++  
        }  
        for k, v := range results { //查询出来的数组  
                fmt.Println(k, v)  
        }  
}  
*/